package ruifeng.zcj.communcationring.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;

import huanxin.ui.VideoCallActivity;
import huanxin.ui.VoiceCallActivity;
import ruifeng.zcj.communcationring.R;
import ruifeng.zcj.communcationring.base.App;

/**
 * zcjqqw
 * zcj
 *
 * zcjqq
 * zcj
 * Created by Administrator on 2016/8/23 0023.
 */
public class Test extends Fragment {

    @Override
    public void onStart() {
        super.onStart();
        // cc();
//        register();
    }

    public void cc() {
        EMChatManager.getInstance().login("zcjqq", "zcj", new EMCallBack() {

            @Override
            public void onSuccess() {
                System.out.println("成功");
//                startVoiceCall();
              //  startVideoCall();
            }

            @Override
            public void onProgress(int progress, String status) {
            }

            @Override
            public void onError(final int code, final String message) {
                System.out.println("失败" + message);
                System.out.println("失败" + code);

            }
        });
    }

    /**
     * 拨打语音电话
     */
    public void startVoiceCall() {
        if (!EMChatManager.getInstance().isConnected()) {
            Toast.makeText(getActivity(), R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
        } else {
            startActivity(new Intent(getActivity(), VoiceCallActivity.class).putExtra("username", "123")
                    .putExtra("isComingCall", false));

        }
    }

    /****
     * 视频通话
     **/
    protected void startVideoCall() {
        if (!EMChatManager.getInstance().isConnected())
            Toast.makeText(App.getAppContext(), R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
        else {// 拨打视频
            startActivity(new Intent(getActivity(), VideoCallActivity.class).putExtra("username", "123")
                    .putExtra("isComingCall", false));
        }
    }

    /**
     * 注册
     */
    public void register() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    // 调用sdk注册方法
                    EMChatManager.getInstance().createAccountOnServer("12345", "123456");
                } catch (final EaseMobException e) {
                    int errorCode = e.getErrorCode();
                    if (errorCode == EMError.NONETWORK_ERROR) {
                        System.out.println("zcj" + EMError.NONETWORK_ERROR);
                    } else if (errorCode == EMError.USER_ALREADY_EXISTS) {
                        System.out.println("zcj" + EMError.USER_ALREADY_EXISTS);
                    } else if (errorCode == EMError.UNAUTHORIZED) {
                        System.out.println("zcj" + EMError.UNAUTHORIZED);
                    } else if (errorCode == EMError.ILLEGAL_USER_NAME) {
                        System.out.println("zcj" + EMError.ILLEGAL_USER_NAME);
                    } else {
                        System.out.println("zcj" + EMError.NONETWORK_ERROR);
                    }
                }
            }
        }).start();

    }

    /****
     * 退出登录
     * **/
    public static void logout() {
        EMChatManager.getInstance().logout(new EMCallBack() {

            @Override
            public void onSuccess() {
                Toast.makeText(App.getAppContext(),"退出登录成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String error) {
//                Toast.makeText(App.getAppContext(),"退出登录失败",Toast.LENGTH_SHORT).show();
            }
        });
    }

}
